import React from 'react'
import PropTypes from 'prop-types'
import {connect} from 'dva'
import {routerRedux} from 'dva/router'
import {Page} from 'components'
import queryString from 'query-string'
import Filter from './components/Filter'
import List from './components/List'
import Modal from './components/Modal'
import {Card} from 'antd'

const duplicateFriend = ({location, dispatch, loading, duplicateFriend}) => {
  const {query, pathname} = location
  const {list, pagination, modalVisible, detailList} = duplicateFriend
  const handleRefresh = (newQuery) => {
    dispatch(routerRedux.push({
      pathname,
      search: queryString.stringify({
        ...query,
        ...newQuery,
      }),
    }))
  }

  const filterProps = {
    filter: {
      ...query,
    },
    onFilterChange (value) {
      handleRefresh({
        ...value,
        current: 1,
      })
    },
  }

  const listProps = {
    rowKey: 'id',
    dataSource: list,
    loading: loading.effects['duplicateFriend/query'],
    pagination,
    location,
    onChange (page) {
      handleRefresh({
        current: page.current,
        size: page.pageSize,
      })
    },
    showModal (record) {
      dispatch({
        type: 'duplicateFriend/duplicateFriendDetail',
        payload: {
          wxId: record.wxId
        }
      })
      dispatch({
        type: 'duplicateFriend/showModal',
      })
    },
  }

  const modalProps = {
    loading,
    detailList,
    modalProps: {
      visible: modalVisible,
      width: 1080,
      destroyOnClose: true,
      maskClosable: false,
      confirmLoading: loading.effects[`duplicateFriend/duplicateFriendDetail`],
      title: '重复好友详情',
      wrapClassName: 'vertical-center-modal',
      footer: null,
      onCancel () {
        dispatch({
          type: 'duplicateFriend/hideModal',
        })
      },
    },
  }

  return (
    <div>
      <Card style={{marginBottom: 12}}>
        <Filter {...filterProps} />
      </Card>
      <Page inner>
        <List {...listProps} />
      </Page>
      <Modal {...modalProps}/>
    </div>
  )
}

duplicateFriend.propTypes = {
  duplicateFriend: PropTypes.object,
  location: PropTypes.object,
  loading: PropTypes.object,
}
export default connect(({duplicateFriend, loading}) => ({duplicateFriend, loading}))(duplicateFriend)
